package com.example.academic_affairs_system.utils.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.io.File;
import java.util.Collections;

/**
 * Mp 数据库代码生成器
 */
public class MybatisPlusGeneratorCode {
  private static final String MASTER =
      "jdbc:mysql://127.0.0.1:4096/academic_affairs_system?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&allowMultiQueries=true";

  private static final String USERNAME = "root", PASSWORD = "gzkemays";

  private static final String PROJECT_PATH =
      System.getProperty("user.dir")
          + File.separator
          + "src"
          + File.separator
          + "main"
          + File.separator
          + "java";

  private static final String MAPPER_XML_PATH =
      System.getProperty("user.dir")
          + File.separator
          + "src"
          + File.separator
          + "main"
          + File.separator
          + "resources"
          + File.separator
          + "mapper";

  public static void main(String[] args) {
    //    System.out.println(PROJECT_PATH);
    FastAutoGenerator.create(MASTER, USERNAME, PASSWORD)
        .globalConfig(
            builder -> {
              builder
                  .author("hjx") // 设置作者
                  .enableSwagger()
                  .outputDir(PROJECT_PATH); // 指定输出目录
            })
        .packageConfig(
            builder -> {
              builder
                  .parent("com.example") // 设置父包名
                  .moduleName("academic_affairs_system") // 设置父包模块名
                  .pathInfo(
                      Collections.singletonMap(OutputFile.xml, MAPPER_XML_PATH)); // 设置mapperXml生成路径
            })
        .strategyConfig(
            builder -> { // 设置需要生成的表名
              builder
                  .entityBuilder()
                  .enableLombok()
                  .enableChainModel()
                  .addTableFills(new Column("create_time", FieldFill.INSERT))
                  .logicDeleteColumnName("enabled");
            })
        .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
        .execute();
  }
}
